import { REST } from '@donnu-prod-practice/api-types';
import { SortDirection } from '@donnu-prod-practice/types';

import { models } from '../../db';
import { ControllerType } from '../../types/types';

/* -------------------------------------------------------------------------- */
/*                                 CONTROLLER                                 */
/* -------------------------------------------------------------------------- */

export const getCountryController: Controller = async (req, res, _nest) => {
  return res.json(prepareResponse(await selectCountryList()));
};

/* -------------------------------------------------------------------------- */
/*                                / CONTROLLER                                */
/* -------------------------------------------------------------------------- */

/* -------------------------------------------------------------------------- */
/*                                   QUERIES                                  */
/* -------------------------------------------------------------------------- */

const selectCountryList = () =>
  models.Country.Model.findAll({
    order: [['id', SortDirection.asc]],
  });

/* -------------------------------------------------------------------------- */
/*                                  / QUERIES                                 */
/* -------------------------------------------------------------------------- */

/* -------------------------------------------------------------------------- */
/*                                TRANSFORMERS                                */
/* -------------------------------------------------------------------------- */

const prepareResponse = (countryList: ReadonlyArray<models.Country.Type>): Response => countryList.map(prepareCountry);

/* -------------------------------------------------------------------------- */

const prepareCountry = ({
  id,
  name,
  rate,
  residentRequests,
  nonResidentRequests,
}: models.Country.Type): Response[number] => ({
  id,
  name,
  rate,
  residentRequests,
  nonResidentRequests,
});

/* -------------------------------------------------------------------------- */
/*                               / TRANSFORMERS                               */
/* -------------------------------------------------------------------------- */

/* -------------------------------------------------------------------------- */
/*                                    TYPES                                   */
/* -------------------------------------------------------------------------- */

type Params = REST.country.get.Params;
type Query = REST.country.get.Query;
type Body = REST.country.get.Body;
type Response = REST.country.get.Response;

type Controller = ControllerType<Params, Query, Body, Response>;

/* -------------------------------------------------------------------------- */
/*                                   / TYPES                                  */
/* -------------------------------------------------------------------------- */
